<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
    <style>
        p {
            width: 200px;
            height: 500px;
            border: 1px solid red;
        }

        div {
            width: 4rem;
            height: 4rem;
            border: 1px solid black;

            text-align: center;
            font-size: 3rem;
            color: black;
            position: fixed;
            right: 1rem;
            bottom: 1rem;

            cursor: pointer;
        }
    </style>
    <h4>6.设计一个程序，点击回到顶部按钮时，不是瞬时回到顶部，而是在1秒内逐渐回到顶部。</h4>
    <p>firstfirstfirst</p>
    <p>secondsecondsecond</p>
    <p>thirdthirdthirdthird</p>
    <div onclick="sto()">↑</div>
    <script type="text/javascript">
        function sto() {
            let temp = window.scrollY;
            let scrollLength = temp / 100;
            let timer = setInterval(function () {
                temp -= scrollLength
                window.scrollTo(0, temp);
                if (temp <= 0) {
                    clearInterval(timer)
                }
            }, 10)
        }
    </script>
</body>

</html>